import * as echarts from '../../../components/ec-canvas/echarts';
let chart = null;
function initChart(canvas, width, height, dpr) {
  chart = echarts.init(canvas, null, {
    width: width,
    height: height,
    devicePixelRatio: dpr // new
  });
  canvas.setChart(chart);
  let setLabel = (data) => {
    let opts = [];
    for (let i = 0; i < data.length; i++) {
        let item = {};
        item.name = data[i].name;
        item.value = data[i].value;
        item.label = {
            normal: {
              textStyle: {
                color: color[i]  // 改变标示文字的颜色
            },
                //控制引导线上文字颜色和位置,此处a是显示文字区域，b做一个小圆圈在引导线尾部显示
                show: true,
                //a和b来识别不同的文字区域
                formatter: [
                  '{a|{d}% }',
                  '{b|}' 
              ].join('\n'), //用\n来换行
                rich: {
                    a: {
                        left: 20,
                        padding: [10, -10, -20, -15]
                    },
                    b: {
                        height: 6,
                        width: 6,
                        lineHeight: 0,
                        marginBottom: 10,
                        padding: [0, -6],
                        borderRadius: 6,
                        backgroundColor: color[i], // 圆点颜色和饼图块状颜色一致
                    }
                },
            }
        }
        opts.push(item)
    }
    return opts;
}
let data = [
  {value: 7, name: '7%'},
  {value: 18, name: '18%'},
  {value: 25, name: '25%'},
  {value: 30, name: '30%'},
  {value: 20, name: '20%'},
];
let color =  ['#B7D7FA', '#FFB4CE', '#FFA733', '#FFD41A', '#EAB7FA'];
  var option = {
    color:color,
    series: [
      {
        name: '面积模式',
        type: 'pie',
        radius: [60, 100],
        center: ['50%', '50%'],
        avoidLabelOverlap: false,
        labelLine: {
            normal: {
                show: true,
                length:10, // 第一段线 长度
                length2:10, // 第二段线 长度
                align: 'center'
            },
            emphasis: {show: true }
        },
        roseType: 'area',
        data:setLabel(data)
    }
    ]
  };
  chart.setOption(option);
  return chart;
}
const app = getApp();
import {cartAddBox,noticeNews} from '../../../api/scanCode.js';
var timer = null;
Page({
  data: {
    comUrl:'https://sc.expresscare.cn/uploads/attach/wxa/',
    isAuto: false, //是否自动授权；
    iShidden: true, //是否隐藏；
    isGoIndex: false, //是否返回首页；
    params:{
      cartNum:1,
      new:0,
      productId:1,
      productCode:'',
      uniqueId:''
      //uniqueId:`cart${app.globalData.userInfo['uid']}`
    },
    // 统计图表
    ec: {
      onInit: initChart
    },
    noticeShow:true,
    noticeText:'',
  },
  scanCodeClick(){
    var that = this;
    if (!app.globalData.isLog) {
      this.setData({iShidden: false });
    } else {
      wx.scanCode({
        scanType:'pdf417',
        success (res) {
          console.log(res.result)
          that.setData({
            ['params.productCode']:res.result
          });
          that.cartAdd();
        },
        fail(res){
          // app.Tips({title: res.errMsg});
        },
      })
    }
  },
  cartAdd(){
    cartAddBox(this.data.params).then(res => {
      wx.navigateTo({
        url: `../result/index?type=load&cartId=${res.data.cartId}`,
      });
    }).catch(res => {
      // app.Tips({title: res});
      wx.navigateTo({
        url: '../result/index?type=nodata',
      });
    });
  },
  useClick(){
    wx.navigateTo({
      url: '../useDesc/index',
    });
  },
   
  animateNotice(arr){
    var num = 0;
    if(arr.length>0){
      this.setData({ 
        noticeText:arr[num].info,
        noticeShow:true
      });
      timer = setInterval(() => {
        if( num == 0){this.setData({ noticeShow:false });}
        num++;
        setTimeout(() => { this.setData({ noticeShow:false });}, 3000);
        if(num > arr.length-1){ num = 0}
        this.setData({ 
          noticeShow:true,
          noticeText:arr[num].info 
        });
      }, 3500);
     }
  },
  getNews(){
    noticeNews().then(res=>{
      this.animateNotice(res.data);
    });
  },
  onLoad: function (options) {   
      this.getNews(); 
  },
  onLoadFun: function (e) {
   
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload(){
    clearInterval(timer);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})